<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				审批
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form-model ref="ruleForm" :model="form" :rules="rules" style="margin-top:50px;">
					<a-form-model-item label="审核状态" prop="isAdopt">
						<a-radio-group name="radioGroup" v-model="form.isAdopt">
							<a-radio :value="true">通过</a-radio>
							<a-radio :value="false">驳回</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="审核意见" prop="comment">
						<a-input type="textarea" :auto-size="global.textareaHeight" v-model="form.comment" />
					</a-form-model-item>
					<a-form-model-item label="" class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">提交</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-model-item>
				</a-form-model>
			</div>
		</div>
	</div>
</template>

<script>
	import Cache from '@/utils/cache';
	import {
		ManageClubAnnualAuditFlowDoExam,
		UserClubAnnualAuditFlowDoExam,
		GetActivityFileList,
		DeleteActivityFile
	} from '@/api/follow';
	export default {
		props: {
			text: Object,
			textArr: Array,
			isShow: Boolean
		},
		watch: {
			text: function(text) {
				// this.getList();
			},
			form: {
				handler: function(newVal, oldVal) {
					this.rules.comment[0].required = !newVal.isAdopt
				},
				deep: true
			}
		},
		data() {
			return {
				oldList: [],
				formLayout: 'horizontal',
				form: this.$form.createForm(this, {
					name: 'coordinated'
				}),
				DepartmenDropdowntList: [],
				form: {
					isAdopt: true,
					comment: ''
				},
				rules: {
					isAdopt: [{
						required: true,
						message: '请选择审核状态',
						trigger: 'change'
					}],
					comment: [{
						required: false,
						message: '请输入审核意见',
						trigger: 'change'
					}]
				},
				requestUrl: this.global.requestUrl,
				token: Cache.get('token'),
			};
		},
		created() {},
		methods: {
			async getList() {
				let res = await GetActivityFileList({
					activityID: this.text.ActivityID,
					type: 2
				});
				let res1 = await GetActivityFileList({
					activityID: this.text.ID,
					type: 2
				});
				this.oldList = res.data.data.length == 0 ? res1.data.data : res.data.data;
			},
			async deleteFile(item) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: `您确定删除该附件吗？`,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let res = await DeleteActivityFile({
							id: item.ID
						});
						if (res.data.code == 0) {
							that.oldList.splice(that.oldList.findIndex(m => m.ID === item.ID), 1);
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			async downloadFile(item) {
				let hrefUrl = this.requestUrl + '/Manage/ActivityFile/DownFile?userToken=' + encodeURIComponent(this
					.token) + '&id=' + item.ID;
				window.open(hrefUrl, '_blank');
			},
			closeFunction(data) {
				this.$emit('closeFun', data);
				this.$refs.ruleForm.resetFields();
			},
			handleSubmit(e) {
				this.$refs.ruleForm.validate(async valid => {
					if (valid) {
						let res = {};
						let that = this
						let data = this.form;
						let successNum = 0;
						let errorList = [];
						console.log(this.$route.query.followId)
						if (this.$route.query.followId) {
							data.id = this.$route.query.followId
							res = await ManageClubAnnualAuditFlowDoExam(data);
							if (res.data.code === 0) {
								this.$message.success(res.data.msg);
								this.closeFunction('1');
							} else {
								this.$message.error(res.data.msg);
							}
						} else {
							if (this.textArr && this.textArr.length > 0) {
								for (let i = 0; i < this.textArr.length; i++) {
									data.id = this.textArr[i].ID
									res = await ManageClubAnnualAuditFlowDoExam(data);
									if (res.data.code == 0) {
										successNum++
									} else {
										errorList.push(this.textArr[i].ActivityTitle || this.textArr[i]
											.TypeItemName)
									}
								}
								const h = this.$createElement;
								this.$info({
									title: '审核结果',
									content: h('div', {}, [
										h('p', `成功审核了${successNum}条数据`),
										h('p', errorList.length > 0 ?
											`失败了${errorList.length}条数据，失败的活动为${errorList.join(',')}` :
											'')
									]),
									onOk() {
										that.closeFunction('1');
									}
								});
								// this.$message.success('已完成');
								// this.closeFunction('1');
							} else {
								console.log('老师审核')
								data.id = this.text.ID
								res = await UserClubAnnualAuditFlowDoExam(data);
								if (res.data.code === 0) {
									this.$message.success(res.data.msg);
									this.closeFunction('1');
								} else {
									this.$message.error(res.data.msg);
								}
							}
						}

						
					} else {
						return false;
					}
				});
			}
		}
	};
</script>

<style lang="less" scoped>
	/deep/.ant-form-item {
		margin: 20px 50px;
		display: flex;
	}

	/deep/.ant-form-item-label {
		width: 100px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	.ant-form-item {
		margin-bottom: 10px;
	}

	/deep/.ant-form {
		margin-top: 50px;
		padding-bottom: 50px;
	}
</style>
